/**
 * 
 */
package code.google.qinglan.min;

import java.io.FileInputStream;
import java.util.List;

import code.google.qinglan.file.FileHelper;

import com.yahoo.platform.yui.compressor.YUICompressor;

/**
 * @author wxq
 * 
 */
public class CompressEngin {

	private CompressJS compressJS;

	private List<CompressUnit> compressUnitList;

	/**
	 * 
	 */
	public CompressEngin() {

	}

	public void doCompress() {
		for (CompressUnit c : compressUnitList) {
			System.out.println("正在处理" + c.getName());
			String tempFlisName = "";
			if (c.getLevel() == 0) {
				tempFlisName = c.getTargetName();
			} else {
				tempFlisName = c.getTargetName()
						.replaceAll("\\.js$", "temp.js");
			}
			try {
			FileHelper f = new FileHelper();
			f.setEncode(c.getEncode());
			compressJS.setFh(f);
			compressJS.mergeJS(c.getFileListName(), tempFlisName, false);
			if (c.getLevel() != 0) {
				String[] a = { "--type", "js", "--charset", c.getEncode(),
						tempFlisName, "-o", c.getTargetName() };
				YUICompressor.main(a);
				f.deleteFile(tempFlisName);
			}
				System.out.println("成功输出文件：" + c.getTargetName() + " Size ： "+ new FileInputStream(c.getTargetName()).available() +"Bytes     Finish!");
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("处理完毕");
		}
	}

	public CompressJS getCompressJS() {
		return compressJS;
	}

	public void setCompressJS(CompressJS compressJS) {
		this.compressJS = compressJS;
	}

	public List<CompressUnit> getCompressUnitList() {
		return compressUnitList;
	}

	public void setCompressUnitList(List<CompressUnit> compressUnitList) {
		this.compressUnitList = compressUnitList;
	}

}
